package yxy.game.pm2.bean.db.dao;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import yxy.apple.database.BaseDAO;
import yxy.apple.database.DAOAccountManager;
import yxy.apple.database.DAOManager;
import yxy.game.pm2.bean.db.module.server;

public class MySQLServerDAO extends BaseDAO<server> {

	public static server get(int id) {
		return get(DAOManager.getGameH(), server.class, id);
	}

	public static server getFromDAOAccountManager(int id) {
		return get(DAOAccountManager.getGameH(), server.class, id);
	}

	public static boolean add(server db) {
		return MySQLServerDAO.add(DAOManager.getGameH(), db);
	}
//
//	public static boolean update(server db) {
//		return MySQLServerDAO.update(DAOManager.getGameH(), db);
//	}

	public static List<server> getListByAreaIds(String areaIds) {
		String sql = "from server where areaId in (" + areaIds + ") order by sid asc";
		return query(DAOManager.getGameH(), sql, null);
	}

	public static List<server> getListByClusterId(int clusterId) {
		String sql = "from server where clusterId = " + clusterId + " order by sid asc";
		return query(DAOManager.getGameH(), sql, null);
	}

	//根据开服时间获取
	public static List<server> getListByOpenTime(int clusterId, String openTime) {
		String sql = "from server where clusterId = " + clusterId + " and openTime <= '" + openTime + "' order by sid asc";
		return query(DAOManager.getGameH(), sql, null);
	}

	public static List<server> all() {
		String sql = "from server order by id asc";
		return query(DAOManager.getGameH(), sql, null);
	}


	public static server getNew() {
		String sql = "from server where state = 3 order by id desc";
		
		List<server> dbs = query(DAOManager.getGameH(), sql, null);
//		server db = queryOne(DAOManager.getGameH(), sql, null);
		if(dbs.size() == 0) {
			sql = "from server where state = 2 order by id desc";
			dbs = query(DAOManager.getGameH(), sql, null);
//			db = queryOne(DAOManager.getGameH(), sql, null);
		}
		if(dbs.size() == 0) {
			sql = "from server order by id desc";
			dbs = query(DAOManager.getGameH(), sql, null);
//			db = queryOne(DAOManager.getGameH(), sql, null);
		}
		return dbs.get(0);
	}

	public static server getNew(ArrayList<String> areaIdList) {
		if (areaIdList == null || areaIdList.size() < 1) return null;

		String now = DateTime.now().toString();

		String areaIds = String.join(",", areaIdList);
		String sql = "from server where areaId in (" + areaIds + ") and openTime <= '" + now + "' and state = 3 order by id desc";

		List<server> dbs = query(DAOManager.getGameH(), sql, null);
//		server db = queryOne(DAOManager.getGameH(), sql, null);
		if(dbs.size() == 0) {
			sql = "from server where areaId in (" + areaIds + ") and openTime <= '" + now + "' and state = 2 order by id desc";
			dbs = query(DAOManager.getGameH(), sql, null);
//			db = queryOne(DAOManager.getGameH(), sql, null);
		}
		if(dbs.size() == 0) {
			sql = "from server where areaId in (" + areaIds + ")  and openTime <= '" + now + "' order by id desc";
			dbs = query(DAOManager.getGameH(), sql, null);
//			db = queryOne(DAOManager.getGameH(), sql, null);
		}
		return dbs.size() > 0 ? dbs.get(0) : null;
	}
}
